package Solution.problem090.SubsetsII;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @program Leetcode
 * @description:
 *
 * Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
 *
 * Note: The solution set must not contain duplicate subsets.
 *
 * Example:
 *
 * Input: [1,2,2]
 * Output:
 * [
 *   [2],
 *   [1],
 *   [1,2,2],
 *   [2,2],
 *   [1,2],
 *   []
 * ]
 *
 *
 * @author: lishangsheng
 * @create: 2019/06/28 09:35
 */
public class Solution {

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> result=new ArrayList<>();
        List<Integer> list=new ArrayList<>();
        Arrays.sort(nums);
        backtracking(result, nums, list, 0);
        return result;
    }

    private void backtracking(List<List<Integer>> result,int nums[],List<Integer> list,int start) {
        result.add(new ArrayList<>(list));
        for(int i=start;i<nums.length;i++){
           if(i>start&&nums[i]==nums[i-1])
                continue;
            list.add(nums[i]);
            backtracking(result, nums, list, i+1);
            list.remove(list.size()-1);

        }
    }

    @Test
    public void test(){
        int[] nums=new int[]{1,2,2};
        List<List<Integer>> lists=subsetsWithDup(nums);
        System.out.println(lists);
    }
}
